package com.cskaoyan.app.controller;

import com.cskaoyan.app.bean.common.BasePageInfo;
import com.cskaoyan.app.bean.common.BaseRespVo;
import com.cskaoyan.app.bean.common.CommonData;
import com.cskaoyan.app.bean.po.MarketKeyword;
import com.cskaoyan.app.service.AdminKeywordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author Shuuka
 * @email 1712430536@qq.com
 * @date 2022/12/29 22:43
 */
@RestController
@RequestMapping("admin/keyword")
public class AdminKeywordController {

    @Autowired
    AdminKeywordService adminKeywordService;

    @GetMapping("list")
    public BaseRespVo<CommonData<MarketKeyword>> list(BasePageInfo basePageInfo,String keyword,String url){
        CommonData<MarketKeyword> data = adminKeywordService.list(basePageInfo,keyword,url);
        return BaseRespVo.ok(data);
    }

    @PostMapping("update")
    public BaseRespVo<Object> update(@RequestBody @Validated MarketKeyword marketKeyword){
        marketKeyword= adminKeywordService.update(marketKeyword);
        if(marketKeyword == null){
            return BaseRespVo.invalidData("更新失败");
        }
        return BaseRespVo.ok(marketKeyword);
    }

    @PostMapping("create")
    public BaseRespVo<Object> create(@RequestBody @Validated MarketKeyword marketKeyword){
        marketKeyword = adminKeywordService.create(marketKeyword);
        if(marketKeyword == null){
            return BaseRespVo.invalidData("更新失败");
        }
        return BaseRespVo.ok(marketKeyword);
    }

    @PostMapping("delete")
    public BaseRespVo<Object> delete(@RequestBody MarketKeyword marketKeyword){
        Integer row = adminKeywordService.delete(marketKeyword);
        if(row != 1){
            return BaseRespVo.invalidParameter("删除失败");
        }
        return BaseRespVo.ok();
    }

}
